package com.jackrain.nea.fc.ps.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.ViewCproDistribQueryCmd;
import com.jackrain.nea.fc.ps.mapper.ViewCproDistribMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * “商品档案接口” FC-->货品网站
 *
 * @author WSL
 * @create 2018/1/19
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "ps")
public class ViewCproDistribQueryCmdImpl extends CommandAdapter implements ViewCproDistribQueryCmd {
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCproDistribQueryCmdImpl--param-->{}",param);

        if ((null == param) || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        // Mapper
        ViewCproDistribMapper viewCproDistribMapper;
        try {
            viewCproDistribMapper = ApplicationContextHandle.getBean(ViewCproDistribMapper.class);
        }catch (Exception e){
            throw new NDSException("服务启动错误！");
        }

        // 根据param查询条件 查询【配销中心商品档案】表
        List<HashMap> list = viewCproDistribMapper.selectProvider(param.getJSONArray("IDS").toArray());
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap: list) {

            // 配销中心 CP_C_DISTRIB_ID
            String distribId = viewCproDistribMapper.getDistribEcode(TypeUtils.castToLong(hashMap.get("CP_C_DISTRIB_ID")));
            hashMap.put("CP_C_DISTRIB_ID",distribId);

            // 供应商 CP_C_SUPPLIER_ID
            String supplier = viewCproDistribMapper.getSupplierEcode(TypeUtils.castToLong(hashMap.get("CP_C_SUPPLIER_ID")));
            hashMap.put("CP_C_SUPPLIER_ID",supplier);

            // 启用 ISACTIVE
            if ("Y".equals(hashMap.get("ISACTIVE"))){
                hashMap.put("ISACTIVE",0);
            }else{
                hashMap.put("ISACTIVE",1);
            }

            // 年份 PROYEAR
            String proYear = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("PROYEAR")));
            hashMap.put("PROYEAR",proYear);

            // 季节 PROSEA
            String proSea = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("PROSEA")));
            hashMap.put("PROSEA",proSea);

            // 性别 SEX
            String sex = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("SEX")));
            hashMap.put("SEX",sex);

            // 企划大类 LARGECLASS
            String largeClass = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("LARGECLASS")));
            hashMap.put("LARGECLASS",largeClass);

            // 供应商品牌 SUPBRAND
            String supBrand = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("SUPBRAND")));
            hashMap.put("SUPBRAND",supBrand);

            // 商品来源 PROSOURCE
            String proSource = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("PROSOURCE")));
            hashMap.put("PROSOURCE",proSource);

            // 采购性质 PURNATURE
            String purNature = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("PURNATURE")));
            hashMap.put("PURNATURE",purNature);

            // 采购员 BUYER
            String buyer = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("BUYER")));
            hashMap.put("BUYER",buyer);

            // 商品性质 PRONATURE
            String proNature = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("PRONATURE")));
            hashMap.put("PRONATURE",proNature);

            // 推广类型 PROMOTIONTYPE
            String proMotionType = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("PROMOTIONTYPE")));
            hashMap.put("PROMOTIONTYPE",proMotionType);

            // 产品线 PROLINE
            String proLine = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("PROLINE")));
            hashMap.put("PROLINE",proLine);

            // 价格带 PRICEBAND
            String priceBand = viewCproDistribMapper.getProDimEcode(TypeUtils.castToLong(hashMap.get("PRICEBAND")));
            hashMap.put("PRICEBAND",priceBand);

            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);

        }
        vh.put("data",resultJson);
        return vh;
    }

}
